package cn.burnit.view;


import cn.burnit.pojo.Reader;
import cn.burnit.service.ReaderService;
import cn.burnit.utils.ReaderTableModel;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 查看所有的读者
 */
public class CheckAllReaderForm extends JFrame {


    private JButton btn_Cancel,btn_Edit;
    private JScrollPane scrollPane;
    private static ReaderTableModel model;
    private static JTable table;
    private  JPanel panel;
    private JLabel sel;
    private static ReaderService readerService=new ReaderService();


    public CheckAllReaderForm() {

        Container container = getContentPane();
        container.setLayout(null);
        btn_Edit=new JButton("编辑");
        btn_Edit.setBounds(280,30,80,20);
        btn_Cancel=new JButton("关闭");
        btn_Cancel.setBounds(400,30,80,20);

        btn_Cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        btn_Edit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //判断是否被选中
                int index=table.getSelectedRow();
                if (-1==index){
                    JOptionPane.showMessageDialog(null,"编辑前先选中一行");
                    return;
                }
                Reader reader=model.readers.get(index);
                EditDialog ed = new EditDialog(new CheckAllReaderForm());
                ed.readerIdField.setText(String.valueOf(reader.getId()));
                ed.readerNameField.setText(reader.getReadername());
                ed.readerTypeField.setText(reader.getReadertype());
                ed.sexField.setText(reader.getSex());
                ed.max_numField.setText(String.valueOf(reader.getMax_num()));
                ed.days_numField.setText(String.valueOf(reader.getDays_num()));

            }
        });


        model = new ReaderTableModel();
        table=new JTable(model);
        table.getColumnModel().getColumn(0).setHeaderValue("读者编号");
        table.getColumnModel().getColumn(1).setHeaderValue("读者姓名");
        table.getColumnModel().getColumn(2).setHeaderValue("读者类别");
        table.getColumnModel().getColumn(3).setHeaderValue("读者性别");
        table.getColumnModel().getColumn(4).setHeaderValue("可借书数");
        table.getColumnModel().getColumn(5).setHeaderValue("可借天数");

        //table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //table.getSelectionModel().setSelectionInterval(0, 0);
        panel = new JPanel();
        sel = new JLabel("暂未选中");
        panel.add(sel);
        panel.setBounds(300,80,200,20);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                //获取选中的行
                int row = table.getSelectedRow();
                //通过选中的行获取这一行的数据 到ReaderTableModel中获取对应的对象
                Reader reader = model.readers.get(row);
                //显示选中的内容
                sel.setText("你当前选中的是："+reader.getReadername());
            }
        });
        scrollPane=new JScrollPane(table);
        scrollPane.setBounds(20,120,750,400);

        container.add(btn_Edit);
        container.add(btn_Cancel);
        container.add(panel,BorderLayout.NORTH);
        container.add(scrollPane);



        setTitle("读者查询");
        setSize(800,600);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    //实时更新数据
    public static void updateTable() {

        model.readers = readerService.list();
        table.updateUI();
        if (!model.readers.isEmpty()){
            table.getSelectionModel().setSelectionInterval(0, 0);
        }

    }



    public static void main(String[] args) {
        new CheckAllReaderForm();
    }

    //编辑弹出框类
    static class EditDialog extends JDialog{
        private JLabel title,readerId,readerName,readerType,sex,max_num,days_num;
        private JTextField readerIdField,readerNameField,readerTypeField,sexField,max_numField,days_numField;
        private JButton update,close;


        public EditDialog(CheckAllReaderForm checkAllReaderForm) {
            super(checkAllReaderForm);
            Container container = getContentPane();
            container.setLayout(null);

            update=new JButton("修改");
            update.setBounds(150,340,60,20);
            close=new JButton("关闭");
            close.setBounds(250,340,60,20);
            close.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });
            update.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int readerid= Integer.parseInt(readerIdField.getText());
                    String readername = readerNameField.getText();
                    String readertype = readerTypeField.getText();
                    String sex = sexField.getText();
                    int max_num = Integer.parseInt(max_numField.getText());
                    int days_num = Integer.parseInt(days_numField.getText());
                    Reader reader = new Reader(readerid,readername,readertype,sex,max_num,days_num);
                    int i=readerService.updateReader(reader);
                    if (i>0){
                        JOptionPane.showMessageDialog(null,"修改成功");
                        EditDialog.this.setVisible(false);
                        updateTable();
                    }
                }
            });

            title=new JLabel("个人信息");
            title.setFont(new Font("宋体", Font.PLAIN, 24));
            title.setBounds(170,10,200,25);
            readerId=new JLabel("读者编号:");
            readerId.setFont(new Font("宋体", Font.PLAIN, 20));
            readerId.setBounds(130,60,100,20);
            readerName=new JLabel("读者名称:");
            readerName.setFont(new Font("宋体", Font.PLAIN, 16));
            readerName.setBounds(130,100,100,20);
            readerType=new JLabel("读者类型:");
            readerType.setFont(new Font("宋体", Font.PLAIN, 16));
            readerType.setBounds(130,140,100,20);
            sex=new JLabel("性别:");
            sex.setFont(new Font("宋体", Font.PLAIN, 16));
            sex.setBounds(162,180,100,20);
            max_num=new JLabel("最大可借数量:");
            max_num.setFont(new Font("宋体", Font.PLAIN, 16));
            max_num.setBounds(100,220,150,20);
            days_num=new JLabel("最大可借天数:");
            days_num.setFont(new Font("宋体", Font.PLAIN, 16));
            days_num.setBounds(100,260,150,20);


            readerIdField=new JTextField();
            readerIdField.setEditable(false);
            readerIdField.setBounds(220,60,130,20);
            readerNameField=new JTextField();
            readerNameField.setEditable(false);
            readerNameField.setBounds(220,100,130,20);
            readerTypeField=new JTextField();
            readerTypeField.setEditable(false);
            readerTypeField.setBounds(220,140,130,20);
            sexField=new JTextField();
            sexField.setEditable(false);
            sexField.setBounds(220,180,130,20);
            max_numField=new JTextField();
            max_numField.setBounds(220,220,130,20);
            days_numField=new JTextField();
            days_numField.setBounds(220,260,130,20);

            container.add(title);
            container.add(readerId);container.add(readerIdField);
            container.add(readerName);container.add(readerNameField);
            container.add(readerType);container.add(readerTypeField);
            container.add(sex);container.add(sexField);
            container.add(max_num);container.add(max_numField);
            container.add(days_num);container.add(days_numField);
            container.add(update);container.add(close);


            setTitle("读者信息");
            setSize(470,470);
            setLocationRelativeTo(null);
            setResizable(false);
            setVisible(true);
        }
    }
}
